package com.it.myd.controller.main;

import com.it.myd.bean.box.Article;
import com.it.myd.bean.box.MsgBoard;
import com.it.myd.bean.box.UserGameInfo;
import com.it.myd.bean.box.game.KaPai.Card;
import com.it.myd.bean.box.world.Item;
import com.it.myd.bean.box.world.Player;
import com.it.myd.bean.main.User;
import com.it.myd.help.FileWatcherExample;
import com.it.myd.module.game.saolei.controller.SaoLeiController;
import com.it.myd.module.game.saolei.service.SaoLeiService;
import com.it.myd.module.main.gameInfo.bean.Game;
import com.it.myd.module.main.gameInfo.service.GameInfoService;
import com.it.myd.service.main.LoginService;
import com.it.myd.service.main.module.article.ArticleService;
import com.it.myd.service.main.module.cardGame.CardShowService;
import com.it.myd.service.main.module.msgboard.MsgBoardService;
import com.it.myd.service.main.module.world.ItemService;
import com.it.myd.service.main.module.world.PlayerService;
import com.it.myd.service.main.module.wuziqi.WuZiQiService;
import jakarta.servlet.ServletContext;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/jump")
public class JumpController {

    @Autowired
    private LoginService loginService;
    @Autowired
    private MsgBoardService msgBoardService;
    @Autowired
    private ArticleService artService;
    @Autowired
    private PlayerService playerService;
    @Autowired
    private ItemService itemService;
    @Autowired
    private WuZiQiService wuZiQiService;
    @Autowired
    private SaoLeiController saoLeiController;
    @Autowired
    private GameInfoService gameInfoService;
    @Autowired
    private SaoLeiService saoLeiService;
    @Autowired
    CardShowService cardShowService;

    @RequestMapping("gologin")
    public String gologin(Model model,HttpServletRequest request) {
        model.addAttribute("user",new User());
        model.addAttribute("a", "123");
        return "main/login";
    }

    @RequestMapping("goSignup")
    public String goSignup(Model model){
        model.addAttribute("user",new User());
        return "main/signup";
    }

    @RequestMapping("goUserCI")
    public String goUserCI(Model model,HttpServletRequest request){
        ServletContext application = request.getServletContext();
        HttpSession session = request.getSession();
        model.addAttribute("user",session.getAttribute("user"));
        return "main/user/userChangerInfo";
    }

    @RequestMapping("goUserCUI")
    public String goUserCUI(){
        return "main/user/userChangerUserImg";
    }

    @RequestMapping("goMain")
    public String goMain(HttpServletRequest request,Model model){
        ServletContext application = request.getServletContext();
        HttpSession session = request.getSession();
        model.addAttribute("user",session.getAttribute("user"));
        model.addAttribute("onflag",0);
        User user = (User) session.getAttribute("user");
        Article article = new Article();
        article.setUser(user);
        List<Article> articleList = artService.selectArticlesHot(article);
        model.addAttribute("articleList",articleList);
        List<Game> gameList = gameInfoService.getGameInfo();
        model.addAttribute("gameList",gameList);
        return "main/main";
    }

    @RequestMapping("goMsgBd")
    public String goMsgBd(HttpServletRequest request,Model model){
        //获取留言板信息
        ServletContext application = request.getServletContext();
        HttpSession session = request.getSession();
        List<MsgBoard> msgBoards = new ArrayList<>();
        User user = (User) session.getAttribute("user");
        user.setOnchose("0");
        MsgBoard mB= new MsgBoard();
        mB.setUser(user);
        mB.setMsg_type(61000);
        msgBoards = msgBoardService.GetMsgBoards(mB);
        application.setAttribute("msgBoards",msgBoards);
        model.addAttribute("user",session.getAttribute("user"));
        model.addAttribute("msgBoards",msgBoards);
        model.addAttribute("onflag",1);
        model.addAttribute("onchose",0);
        return "main/module/msgboard/msgboard";
    }

    @RequestMapping("goMsgBd/hot")
    public String goMsgBdhot(HttpServletRequest request,Model model){
        //获取留言板信息
        HttpSession session = request.getSession();
        List<MsgBoard> msgBoards = new ArrayList<>();
        User user = (User) session.getAttribute("user");
        user.setOnchose("0");
        MsgBoard mB= new MsgBoard();
        mB.setUser(user);
        mB.setMsg_type(61000);
        msgBoards = msgBoardService.GetMsgBoards(mB);
        model.addAttribute("user",session.getAttribute("user"));
        model.addAttribute("msgBoards",msgBoards);
        model.addAttribute("onflag",1);
        model.addAttribute("onchose",0);
        return "main/module/msgboard/msgboard";
    }

    @RequestMapping("goMsgBd/new")
    public String goMsgBdnew(HttpServletRequest request,Model model){
        //获取留言板信息
        HttpSession session = request.getSession();
        List<MsgBoard> msgBoards = new ArrayList<>();
        User user = (User) session.getAttribute("user");
        user.setOnchose("1");
        MsgBoard mB= new MsgBoard();
        mB.setUser(user);
        mB.setMsg_type(61000);
        msgBoards = msgBoardService.GetMsgBoards(mB);
        model.addAttribute("user",user);
        model.addAttribute("msgBoards",msgBoards);
        model.addAttribute("onflag",1);
        model.addAttribute("onchose",1);
        return "main/module/msgboard/msgboard";
    }

    @RequestMapping("goVideo")
    public String goVideo(HttpServletRequest request,Model model){
        HttpSession session = request.getSession();
        ServletContext application = request.getServletContext();
        User user = (User) session.getAttribute("user");
        user.setUser_status("20005");
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        user.setUser_status_time(sdf.format(System.currentTimeMillis()));
        loginService.updateUserStatus(user);

        String videopath = (String) application.getAttribute("videopath");
        model.addAttribute("videopath",videopath);
        model.addAttribute("user",session.getAttribute("user"));
        model.addAttribute("onflag",2);

        session.setAttribute("talklist",null);
        return "main/module/video/video";
    }

    @RequestMapping("goArticle")
    public String goArticle(HttpServletRequest request,Model model){
        HttpSession session = request.getSession();
        model.addAttribute("user",session.getAttribute("user"));
        return "main/module/article/writearticle";
    }

    @RequestMapping("goLookArticle")
    public String goLookArticle(HttpServletRequest request,Model model){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        Article article = new Article();
        article.setUser(user);
        List<Article> articleList = artService.selectArticles(article);
        model.addAttribute("user",session.getAttribute("user"));
        model.addAttribute("articleList",articleList);
        return "main/module/article/articlelist";
    }

    @RequestMapping("goUno")
    public String goUno(HttpServletRequest request,Model model){
        HttpSession session = request.getSession();
        ServletContext application = request.getServletContext();
        User user = (User) session.getAttribute("user");
        List<User> playlist = (List<User>) application.getAttribute("playlist");
        if(playlist == null){
            playlist = new ArrayList<>();
            user.unoPlayInit();
            playlist.add(user);
        }else{
            boolean flag = true;
            for (User u : playlist) {
                if(u.getUser_id()==user.getUser_id()){
                    flag = false;
                    break;
                }
            }
            if(flag){
                user.unoPlayInit();
                playlist.add(user);
            }
        }
        application.setAttribute("playlist",playlist);
        application.setAttribute("unojf",1);
        application.setAttribute("addtwo",0);
        model.addAttribute("playlist",playlist);
        model.addAttribute("user",user);
        return "main/module/lobby/uno";
    }

    @RequestMapping("goWorld")
    public String goWorld(HttpServletRequest request,Model model){
        HttpSession session = request.getSession();
        ServletContext application = request.getServletContext();
        User user = (User) session.getAttribute("user");
        List<Player> list = playerService.getPlayerList();
        boolean flag = true;
        if(list.size() > 0){
            for(Player player: list){
                if(player.getPlayer_info().getUser_id()==user.getUser_id()){
                    flag = false;
                    break;
                }
            }
        }
        if(flag){
            playerService.addPlayer(user);
        }
        List<Item> items = itemService.getBaseItemList();
        model.addAttribute("user",session.getAttribute("user"));
        model.addAttribute("items",items);
        return "main/module/lobby/world";
    }

    @RequestMapping("goAiMsg")
    public String goAiMsg(HttpServletRequest request,Model model){
        //获取留言板信息
        ServletContext application = request.getServletContext();
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        model.addAttribute("user",session.getAttribute("user"));
        return "main/module/aimsg/aimsg";
    }

    @RequestMapping("goMap")
    public String goMap(HttpServletRequest request,Model model){
        //获取留言板信息
        ServletContext application = request.getServletContext();
        HttpSession session = request.getSession();
        model.addAttribute("user",session.getAttribute("user"));
        return "main/module/map/map";
    }

    @RequestMapping("goWuZiQi")
    public String goWuZiQi(HttpServletRequest request,Model model){
        //获取留言板信息
        ServletContext application = request.getServletContext();
        HttpSession session = request.getSession();
        model.addAttribute("user",session.getAttribute("user"));
        List<UserGameInfo> phblist = new ArrayList<>();
        UserGameInfo ugi = new UserGameInfo();
//        ugi.setType("1");
        phblist = wuZiQiService.getUserGameInfo(ugi);
        model.addAttribute("phblist",phblist);
        return "main/module/wuziqi/wuziqi_home";
    }

    @RequestMapping("goWuZiQiRoom")
    public String goWuZiQiRoom(HttpServletRequest request,Model model){
        //获取留言板信息
        ServletContext application = request.getServletContext();
        HttpSession session = request.getSession();
        model.addAttribute("user",session.getAttribute("user"));
        return "main/module/wuziqi/wuziqi_room";
    }

    @RequestMapping("goMCTermInfo")
    public String goMCTermInfo(HttpServletRequest request,Model model){
        HttpSession session = request.getSession();
        model.addAttribute("user",session.getAttribute("user"));
        String content = FileWatcherExample.getSharedOutFileContent();
        model.addAttribute("content",content);
        return "main/module/mcinfo/termInfo";
    }

    @RequestMapping("goSaolei")
    public String goSaolei(HttpServletRequest request,Model model){
        HttpSession session = request.getSession();
        model.addAttribute("user",session.getAttribute("user"));

        User user = (User) session.getAttribute("user");
        user.setUser_status("20004");
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        user.setUser_status_time(sdf.format(System.currentTimeMillis()));
        loginService.updateUserStatus(user);

        saoLeiController.createSaoleiTable(request);

        Map<String,String> map = saoLeiService.getSaoLeiTable();
        model.addAttribute("saoleiMap",map);
        return "main/module/saolei/saolei";
    }

    @RequestMapping("goCardbox")
    public String goCardbox(HttpServletRequest request,Model model){
        HttpSession session = request.getSession();
        model.addAttribute("user",session.getAttribute("user"));
        List<Card> cardList = cardShowService.getCardShowList();
        model.addAttribute("cardList",cardList);
        return "main/module/cardbox/cardbox";
    }

    @RequestMapping("goSeeCard")
    public String goSeeCard(HttpServletRequest request,Model model){
        HttpSession session = request.getSession();
        model.addAttribute("user",session.getAttribute("user"));
        String cardid = request.getParameter("cardid");
        String fimg = request.getParameter("fimg");
        String bimg = request.getParameter("bimg");
        String hwbl = request.getParameter("hwbl");

        model.addAttribute("cardid",cardid);
        model.addAttribute("fimg",fimg);
        model.addAttribute("bimg",bimg);
        model.addAttribute("hwbl",hwbl);
        return "main/module/cardbox/card";
    }
}
